package Java面试周阳.LRU;

import java.util.LinkedHashMap;
import java.util.Map;

public class LRUCacheDemo<K, V> extends LinkedHashMap<K, V> {
    private int capacity;

    /**
     * accessorder
     * true - access-order
     * false - insertion-order
     * @param capacity
     */
    public LRUCacheDemo(int capacity) {
        super(capacity, 0.75F, true);
        this.capacity = capacity;
    }

    @Override
    protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
        return super.size() > capacity;
    }

    public static void main(String[] args) {
        LRUCacheDemo<Integer, String> lru = new LRUCacheDemo<>(3);
        lru.put(1, "a");
        lru.put(2, "b");
        lru.put(3, "c");
        System.out.println(lru.keySet());
        lru.put(4, "e");
        System.out.println(lru.keySet());
        lru.put(3, "c");
        System.out.println(lru.keySet());
    }
}
